/** 
    @file scheduler.h
    @brief Prototypes and types used by the scheduler.
*/

#ifndef WG_SCHEDULER_HEADER
#define WG_SCHEDULER_HEADER

#include <time.h>

/** An event within a schedule. */
typedef struct event event;

/** A schedule of events.  */
typedef struct schedule schedule;

/** A list of schedules. Used inside of the hash table which stores each resource thread's schedule. */
typedef struct schedule_list schedule_list;

struct event {
  struct timespec *timestamp;
  event* next;
  void (*job) (void *);
  void * args;
};

struct schedule {
  event* head;
};

struct schedule_list {
  schedule_list *next;
  unsigned long int key;
  schedule *s;
};

/** Sort a schedule placing the event occuring earliest at the head. */
schedule * sort_schedule(schedule *);

/** Add an event to a schedule.*/
void add_to_schedule(schedule *, event *);

/** Schedule an event to the calling thread's schedule. Takes care of appending the event to the schedule and sorting the schedule. */
void schedule_event(event *);

/**  Removes the next event from the calling thread's schedule and returns it. */
event * next_event();

/** Check if the calling thread's schedule has an event to occur. */
int has_event();

/** Installs a schedule inside the schedule hash table for the calling thread. */
void setup_schedule();

#endif
